/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
import model.Locatie;
import model.Toernooi;

/**
 *
 * @author Gebruiker
 */
public class ToernooiAanmaken extends javax.swing.JFrame {
    Toernooi eenToernooi = new Toernooi();
    /**
     * Creates new form ToernooiAanmaken
     */
    public ToernooiAanmaken() {
        initComponents();
        
        if (!Database.openConnection("fullhouse", "root", "")) {
        System.out.println("Geen verbinding met de database");
        System.exit(0);
        }
        this.vulTabelLocatie();
    }
    
    //private void update_table() {
      //  try {
        //String sql = "select locatie_plaats as plaats, locatie_straat as straat, locatie_huisnummer as huisnr, locatie_postcode as postcode, locatie_max_deelnemers as maxDeelnemers FROM locatie;";
        //ResultSet rs = Database.executeSelectQuery(sql);
        //locatieTable.setModel(DbUtils.resultSetToTableModel(rs));
        //} catch (Exception e) {
        //}
    //}
    
    
    private void vulTabelLocatie() {
        //Ophalen van de toernooien uit de database

        Locatie locatie = new Locatie();
        String[] kolommen = {"Naam", "Plaats", "Straat", "Postcode", "Huisnummer"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);

        int aantalLocaties = locatie.haalAantalLocatiepOp();

        //Lijstje aanmaken voor de tennistoernooien die we uit de database gaan halen. 
        Locatie[] locaties;
        if (aantalLocaties == -1) {
            //return null;
        } else {
            locaties = new Locatie[aantalLocaties];
        }

        try {

            String sql = "SELECT * FROM locatie";
            ResultSet resultaat = Database.executeSelectQuery(sql);

            int teller = 0;
            while (resultaat.next()) {

                String huisnummer = resultaat.getString("locatie_huisnummer");
                String postcode = resultaat.getString("locatie_postcode");

                Locatie tabelVulling = new Locatie(huisnummer, postcode);
                model.addRow(tabelVulling.getInformatie());
            }
        } catch (SQLException e) {
        }

        locatieTable.setModel(model);

    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        Naam = new javax.swing.JTextField();
        Datum = new javax.swing.JTextField();
        Tijd = new javax.swing.JTextField();
        Inleggeld = new javax.swing.JTextField();
        Minaantalspelers = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        postcode = new javax.swing.JLabel();
        huisnummer = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        locatieTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 500));
        getContentPane().setLayout(null);

        jLabel1.setText("Datum");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(37, 64, 58, 14);

        jLabel2.setText("Tijd");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(37, 103, 36, 14);

        jLabel3.setText("Inleggeld");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(37, 143, 58, 14);

        jLabel4.setText("Naam");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(37, 26, 39, 14);

        jLabel5.setText("Min aantal deelenemers");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(37, 182, 143, 14);
        getContentPane().add(Naam);
        Naam.setBounds(190, 21, 103, 25);

        Datum.setText("yyyy-mm-dd");
        getContentPane().add(Datum);
        Datum.setBounds(190, 57, 103, 28);

        Tijd.setText("00:00");
        getContentPane().add(Tijd);
        Tijd.setBounds(190, 96, 103, 29);
        getContentPane().add(Inleggeld);
        Inleggeld.setBounds(190, 136, 103, 28);
        getContentPane().add(Minaantalspelers);
        Minaantalspelers.setBounds(190, 175, 103, 29);

        jButton1.setText("Voeg toernooi toe");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(140, 290, 160, 23);

        postcode.setText("postcode");
        getContentPane().add(postcode);
        postcode.setBounds(40, 220, 80, 14);

        huisnummer.setText("Huisnummer");
        getContentPane().add(huisnummer);
        huisnummer.setBounds(180, 220, 90, 14);

        locatieTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        locatieTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                locatieTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(locatieTable);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(342, 10, 420, 250);

        pack();
    }// </editor-fold>//GEN-END:initComponents

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    eenToernooi.setToernooi_naam(Naam.getText());
    eenToernooi.setToernooi_datum(Datum.getText());
    eenToernooi.setToernooi_tijd(Tijd.getText()); 
    eenToernooi.setToernooi_inleggeld(Inleggeld.getText());
    eenToernooi.setToernooi_minaantalspelers(Minaantalspelers.getText()); 
      //sla de gegevens op in de database
    if(eenToernooi.insert())
      this.dispose();
      
    else
      System.out.println("Persoon toevoegen: opslaan: " + "error bij opslaan");

    }//GEN-LAST:event_jButton1ActionPerformed

    private void locatieTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_locatieTableMouseClicked
        // TODO add your handling code here:
        int row = locatieTable.getSelectedRow();
        huisnummer.setText((String) locatieTable.getModel().getValueAt(row, 2));
        postcode.setText((String) locatieTable.getModel().getValueAt(row, 3));
    }//GEN-LAST:event_locatieTableMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ToernooiAanmaken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ToernooiAanmaken().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Datum;
    private javax.swing.JTextField Inleggeld;
    private javax.swing.JTextField Minaantalspelers;
    private javax.swing.JTextField Naam;
    private javax.swing.JTextField Tijd;
    private javax.swing.JLabel huisnummer;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable locatieTable;
    private javax.swing.JLabel postcode;
    // End of variables declaration//GEN-END:variables
}
